/*
 * txdnet.cn tonydon
 * by netbeans ide 7.2
 */
package cn.txdnet.txweb.app;

import cn.txdnet.txweb.basedao.mybatis.MybatisSqlSessionMgr;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import txdnet.util.Logger;

/**
 *
 * @author tangxiaodong
 */
public class DBConfig {

    private static Class clazz = DBConfig.class;
    private static MybatisSqlSessionMgr mybatisSqlSessionMgr = null;

    /**
     * 得到mybatis sqlsession 管理器
     *
     * @return
     */
    public static final MybatisSqlSessionMgr getMybatisSqlSessionMgr() {
        return DBConfig.mybatisSqlSessionMgr;
    }

    /**
     * 初始化
     */
    public static void init() {
        try {
            Logger.info(clazz, "Init MybatisSqlSessionMgr.");
            mybatisSqlSessionMgr = new MybatisSqlSessionMgr();
            mybatisSqlSessionMgr.initialize();
        } catch (Exception e) {
            Logger.error(clazz, e);
        }
    }

    /**
     * 销毁数据
     */
    public static void destroy() {
        Logger.info(clazz, "Destroy DBConfig ...");
        mybatisSqlSessionMgr.destroy();
        mybatisSqlSessionMgr = null;
        // 卸载db 驱动
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            try {
                Logger.info(clazz, String.format(" Destroy Driver : %s", driver.getClass().getCanonicalName()));
                DriverManager.deregisterDriver(driver);
            } catch (SQLException e) {
                Logger.error(clazz, e);
            }
        }
    }
}
